package com.swgk.core.util;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;

import com.swgk.core.R;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.interfaces.DialogClick;

/**
 * @author : younghow
 * @date : 2021/11/23 15:39
 * description:
 */
public class NewDialogHelper<D> {
    private final View mView;
    private final RoundCornerDialog mRoundCornerDialog;
    private final Context mContext;
    private DialogClick<D> mDialogClick;

    public NewDialogHelper(Context context, @LayoutRes int layout) {
        this.mContext = context;
        mView = View.inflate(context, layout, null);
        mRoundCornerDialog = new RoundCornerDialog(mContext, 0, 0, mView, R.style.RoundCornerDialog);
    }

    public NewDialogHelper(Context context) {
        this.mContext = context;
        mView = View.inflate(context, R.layout.library_dialog_edit, null);
        mRoundCornerDialog = new RoundCornerDialog(mContext, 0, 0, mView, R.style.RoundCornerDialog);
    }

    public NewDialogHelper<D> editDialog(boolean confirmNormal,boolean cancelNormal,String...content){
        mRoundCornerDialog.setCanceledOnTouchOutside(false);
        EditText tvMessage = mView.findViewById(R.id.edi_input);
        TextView tvTitle = mView.findViewById(R.id.tv_title);
        TextView tvLogoutConfirm = mView.findViewById(R.id.tv_logout_confirm);
        TextView tvLogoutCancel = mView.findViewById(R.id.tv_logout_cancel);
        EditTextFilterUtils.setEtFilterAddLength(tvMessage,15);
        tvMessage.setHint(content[1]);
        tvTitle.setText(content[0]);
        tvLogoutConfirm.setText(content[2]);
        tvLogoutCancel.setText(content[3]);
        if(content.length==5){
            if(!TextUtils.isEmpty(content[4])){
                tvMessage.setText(content[4]);
            }
        }
        tvTitle.setVisibility(View.VISIBLE);
        //确定
        tvLogoutConfirm.setOnClickListener(v -> {
            if (confirmNormal){
                mDialogClick.confirmClick(tvMessage.getText().toString(),mRoundCornerDialog);
                mRoundCornerDialog.dismiss();
            }else {
                mDialogClick.confirmClick(tvMessage.getText().toString(),mRoundCornerDialog);
            }
        });
        //取消
        tvLogoutCancel.setOnClickListener(v -> {
            if (cancelNormal){
                mRoundCornerDialog.dismiss();
            }else {
                mDialogClick.cancelClick();
            }
        });
        return this;
    }

    public NewDialogHelper<D> setDialogClick(DialogClick<D> dialogClick){
        this.mDialogClick = dialogClick;
        return this;
    }

    public NewDialogHelper<D> textColor(@IdRes int id, @ColorInt int color){
        TextView textView = mView.findViewById(id);
        textView.setTextColor(color);
        return this;
    }

    public NewDialogHelper<D> background(@IdRes int id, int color){
        mView.findViewById(id).setBackgroundResource(color);
        return this;
    }

    public NewDialogHelper<D> text(@IdRes int id,String s){
        TextView textView = mView.findViewById(id);
        textView.setText(s);
        return this;
    }

    public NewDialogHelper<D> hint(@IdRes int id,String s){
        EditText editText = mView.findViewById(id);
        editText.setHint(s);
        return this;
    }

    public NewDialogHelper<D> hintColor(@IdRes int id,@ColorInt int color){
        EditText editText = mView.findViewById(id);
        editText.setHintTextColor(color);
        return this;
    }

    public NewDialogHelper<D> imageResource(@IdRes int id,int color){
        ImageView editText = mView.findViewById(id);
        editText.setImageResource(color);
        return this;
    }

    public NewDialogHelper<D> onClick(@IdRes int id,DialogClick<D> dialogClick){
        mView.findViewById(id).setOnClickListener(v -> dialogClick.confirmClick("",mRoundCornerDialog));
        return this;
    }

    public NewDialogHelper<D> visible(@IdRes int id,int visible){
        mView.findViewById(id).setVisibility(visible);
        return this;
    }

    public NewDialogHelper<D> canceledOnTouchOutside(boolean flag){
        mRoundCornerDialog.setCanceledOnTouchOutside(flag);
        return this;
    }

    public RoundCornerDialog build(){
        return mRoundCornerDialog;
    }

    public void show(){
        mRoundCornerDialog.show();
    }

    public void cancel(){
        mRoundCornerDialog.cancel();
    }

}
